#include "DrawlistHandler.h"

DrawlistHandler::DrawlistHandler()
{
}


Vector2D DrawlistHandler::AddObject(BaseObject* pObj, e_drawlists drawlistIndex)
{
	Vector2D location(drawlistIndex, 0);
	if(drawlistIndex != NUM_DRAWLISTS)
	{
		location.y = (float)m_vDrawlists[drawlistIndex].push(pObj);
	}

	return location;
}

bool DrawlistHandler::RemoveObjectByIndex(e_drawlists drawlistIndex, int index)
{
	if(drawlistIndex != NUM_DRAWLISTS)
	{
		return m_vDrawlists[drawlistIndex].remove(index);
	}

	return false;
}

bool DrawlistHandler::RemoveObjectByRef(e_drawlists drawlistIndex, BaseObject* pToRemove)
{
	if(drawlistIndex != NUM_DRAWLISTS)
	{
		for(int i = 0; i < m_vDrawlists[drawlistIndex].size(); ++i)
		{
			if(m_vDrawlists[drawlistIndex].getByIndex(i) == pToRemove)
				return m_vDrawlists[drawlistIndex].remove(i);
		}
	}

	return false;
}

short DrawlistHandler::AddWorldBlocks(Block_Info ** pChunkArr, short size)
{
	// verify that pointer is not NULL before continuing
	if(pChunkArr == NULL)
		return 0;

	// add specified chunk elements to the vector, returning the value of the first one to be entered
	short startIndex = -1;
	for(short i(0); i < size; ++i)
	{
		if(i==0)
			startIndex = m_vWorldDrawlist.push(pChunkArr[i]);
		else
			m_vWorldDrawlist.push(pChunkArr[i]);
	}

	return startIndex;
}

bool DrawlistHandler::RemoveWorldBlocks(short index, short numToRemove)
{

	// attempt to remove specified elements from the vector, returning false if any failed
	bool success = true;
	for(short i(index+numToRemove-1); i >= index; --i)
	{
		if(!m_vWorldDrawlist.remove(i))
			success = false;
	}

	return success;
}

//Decommissioned becaused of inoptimization
///*bool DrawlistHandler::RemoveWorldBlocksByRef(Block_Info ** pToRemove, short size)
//{
//	for(int j = 0; j < size; j++) {
//		for(int i = 0; i < m_vWorldDrawlist.size(); ++i)
//		{
//			if(m_vWorldDrawlist.getByIndex(i) == pToRemove[j])
//				return m_vWorldDrawlist.remove(i);
//		}
//	}
//
//	return false;
//}*/